/**
 * 
 */
package org.t2.backstage.module.recharge.service;

import java.util.List;

import org.banish.sql.core.util.QuerySet;
import org.gaming.service.advice.ResponseResult;
import org.gaming.service.service.AbstractService;
import org.gaming.tool.GsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.t2.backstage.module.gameserver.model.GameServer;
import org.t2.backstage.module.gameserver.service.GameServerService;
import org.t2.backstage.module.recharge.model.RechargeCallback;
import org.t2.backstage.util.T2HttpUtil;

import sh.t2.share.apiutil.GameApi;
import sh.t2.share.module.recharge.RechargeApiProtocol.RechargeCallServerRequest;
import sh.t2.share.module.recharge.RechargeApiProtocol.RechargeCallServerResponse;

/**
 * @author YY
 *
 */
@Service
public class RechargeCallbackService extends AbstractService<RechargeCallback> {

	@Autowired
	private GameServerService gameServerService;
	
	
	public RechargeCallback getEntity(long id) {
		return this.repository().getByKeys(id);
	}
	
	
	public List<RechargeCallback> queryCallbacks(QuerySet querySet) {
		return this.repository().getBaseDao().queryListWhere(querySet.getWhere(), querySet.getParams());
	}
	
	/**
	 * 处理充值发货
	 * @param recharge
	 * @return
	 */
	public RechargeStatus handleCallback(RechargeCallback recharge) {
		if(!recharge.isAllCheck()) {
			return RechargeStatus.NOT_CHECK;
		}
		RechargeStatus rechargeStatus = RechargeStatus.NONE;
		GameServer gameServer = null;
		try {
			//通知游戏服进行发货
			gameServer = gameServerService.getByPlatformAndServer(recharge.getPlatformId(), recharge.getServerId());
			RechargeCallServerRequest request = new RechargeCallServerRequest();
			request.orderId = recharge.getGameOrderId();
			request.payMoney = recharge.getMoney();
			request.test = recharge.getTest() == 1;
			request.backstageOrderId = recharge.getId() + "";
			
			logger.info("充值通知发货参数{}", GsonUtil.toJson(request));
			logger.info("充值通知发货服务器{}，{}，{}，{}，{}", gameServer.getId(), gameServer.getServerId(), gameServer.getName(), gameServer.getInternalIp(), gameServer.getHttpPort());
			ResponseResult<RechargeCallServerResponse> response = T2HttpUtil.jsonPost(gameServer, GameApi.rechargeCallback, request, RechargeCallServerResponse.class);
			logger.info("充值通知发货响应{}", GsonUtil.toJson(response));
			if(response == null) {
				recharge.setRemark("服务器未响应");
				rechargeStatus = RechargeStatus.FAIL;
			} else {
				recharge.setGameResponse(GsonUtil.toJson(response));
				if(response.getCode() == 0) {
					//发货成功
					recharge.setGive(1);
					recharge.setRemark("发货成功");
					rechargeStatus = RechargeStatus.SUCCESS;
				} else if(response.getCode() == 1900001) {
					recharge.setGive(2);
					recharge.setRemark(response.getCode() + "-" + response.getMessage());
					rechargeStatus = RechargeStatus.REPEATED;
				} else {
					recharge.setGive(2);
					recharge.setRemark(response.getCode() + "-" + response.getMessage());
					rechargeStatus = RechargeStatus.FAIL;
				}
			}
		} catch (Exception e) {
			logger.error("", e);
			rechargeStatus = RechargeStatus.FAIL;
		} finally {
			this.update(recharge);
		}
		
		if(recharge.getGive() == 1) {
			//成功发货才记录商品购买
		} else {
			//未能正常发货需要走人工补单流程
		}
		return rechargeStatus;
	}
	
	
	public <T> List<T> queryAliasObject(Class<T> clazz, String sql, Object... params) {
		return this.repository().getBaseDao().queryAliasObjects(clazz, sql, params);
	}

	@Override
	public void mergeServer(int mergeDbZoneId) {
		//不必理会
	}
}
